package self.niu.com.resume.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import java.util.ArrayList;
import java.util.List;
import self.niu.com.resume.R;
import self.niu.com.resume.adapter.UserListAdapter;
import self.niu.com.resume.db.User;
import self.niu.com.resume.db.dbHeple.DbHander;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyDataFragment extends Fragment implements View.OnClickListener {
    private View view;
    @ViewInject(R.id.mydata_show_lv)
    private ListView lv;
    @ViewInject(R.id.data_change_tv)
    private CheckBox change;
    @ViewInject(R.id.list_change_tv)
    private TextView dataChange;
    private List<User> datas;
    private UserListAdapter adapter;
    private boolean ischange;
    private Handler handler = new Handler();
    public MyDataFragment() {
        // Required empty public constructor
    }

    public static MyDataFragment getInstance() {
        return new MyDataFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_data, container, false);
        ViewUtils.inject(this, view);
        datas = new ArrayList<>();
        initView();
        getData();
        change.setOnClickListener(this);
        return view;
    }

    private void initView() {

        adapter = new UserListAdapter(getActivity(), datas, ischange);
        lv.setAdapter(adapter);
    }


    //得到数据
    public void getData() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        datas.addAll(DbHander.getInstance().findUserAll());
                        //这里需要使用添加而不是等于
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        ischange = change.isChecked();
        if (ischange) {
            change.setText("完成");
        } else {
            change.setText("修改");
        }
        initView();
    }
}
